package com.litidea.commons.dict;

import static com.litidea.dao.DAOKeys.TABLE_START;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import com.litidea.dataobjects.KValueDO;

/**
 * 动态代码表关系管理
 * 
 */
@SuppressWarnings( { "unchecked" })
public class DynamicDicts {

	static JdbcTemplate jdbc;

	// private static List<DynamicDictBase> dict_base_list;

	private static Map<String, DynamicDictBase> dict_base_map;

	private final static String GET_ALL_DICT_BASE = "select id,name,is_big_value from "
			+ TABLE_START + "dicts";

	private final static String GET_ONE_DICT_DETAIL = "select t.detail_id,t.dict_value from "
			+ TABLE_START + "dict_details t where dict_id=?";

	public static void initCache(JdbcTemplate jdbcTemplate) {
		jdbc = jdbcTemplate;
		refreshAllDicts();
	}

	/**
	 * 刷新所有的代码表
	 */
	private static void refreshAllDicts() {
		List<DynamicDictBase> list = jdbc.query(GET_ALL_DICT_BASE,
				new RowMapper() {
					public Object mapRow(ResultSet rs, int rowNum)
							throws SQLException {
						return new DynamicDictBase(rs.getString("id"), rs
								.getString("name"), rs.getInt("is_big_value"));
					}
				});
		Map<String, DynamicDictBase> keyDicts = new HashMap<String, DynamicDictBase>(
				list.size() * 4 / 3 + 1);
		for (DynamicDictBase dictBase : list) {
			keyDicts.put(dictBase.getId(), dictBase);
			refreshOneDict(dictBase);
		}
		// dict_base_list = list;
		dict_base_map = keyDicts;
	}

	/**
	 * 获取
	 * 
	 * @param dictBase
	 */
	private static void refreshOneDict(DynamicDictBase dictBase) {
		if (dictBase.isBigData())
			return;// 大数据刷新被拒绝
		List<KValueDO> list = jdbc.query(GET_ONE_DICT_DETAIL,
				new Object[] { dictBase.getId() }, new RowMapper() {
					public Object mapRow(ResultSet rs, int rowNum)
							throws SQLException {
						return new KValueDO(rs.getString("detail_id"), rs
								.getString("dict_value"));
					}
				});
		Map<String, String> detailMap = new HashMap<String, String>(
				list.size() * 4 / 3 + 1);
		for (KValueDO kvDO : list) {
			detailMap.put(kvDO.getData(), kvDO.getLabel());
		}
		dictBase.setDetailList(list);
		dictBase.setDetailMap(detailMap);
	}

	/**
	 * 通过一个key刷新代码表
	 * 
	 * @param key
	 */
	public static void refreshOneDictByKey(String key) {
		refreshOneDict(findDictBase(key));
	}

	/**
	 * 获取一个代码表
	 * 
	 * @param key
	 * @return
	 */
	public static DynamicDictBase getOneDictByKey(String key) {
		return findDictBase(key).copyNew();
	}

	/**
	 * 获取一个detail的明细列表
	 * 
	 * @param key
	 * @return
	 */
	public static List<KValueDO> getOneDictDetails(String key) {
		DynamicDictBase dynamicBase = findDictBase(key);
		if (dynamicBase.isBigData()) {
			throw new RuntimeException("big data can not get list");
		} else {
			return dynamicBase.copyNewDetails();
		}
	}

	/**
	 * 获取Double值
	 * 
	 * @param dictId
	 * @param detailId
	 * @return
	 */
	public static JSONObject getJSON(String dictId, String detailId) {
		return findDictBase(dictId).getJSON(detailId);
	}

	/**
	 * 获取Double值
	 * 
	 * @param dictId
	 * @param detailId
	 * @return
	 */
	public static Double getDouble(String dictId, String detailId) {
		return findDictBase(dictId).getDouble(detailId);
	}

	/**
	 * 获取Long值
	 * 
	 * @param dictId
	 * @param detailId
	 * @return
	 */
	public static Long getLong(String dictId, String detailId) {
		return findDictBase(dictId).getLong(detailId);
	}

	/**
	 * 获取Integer
	 * 
	 * @param dictId
	 * @param detailId
	 * @return
	 */
	public static Integer getInteger(String dictId, String detailId) {
		return findDictBase(dictId).getInteger(detailId);
	}

	/**
	 * 获取一个明细
	 * 
	 * @param dictId
	 * @param dictDetails
	 * @return
	 */
	public static String getString(String dictId, String detailId) {
		return findDictBase(dictId).getString(detailId);
	}

	/**
	 * 获取基本信息
	 * 
	 * @param dictId
	 * @return
	 */
	private static DynamicDictBase findDictBase(String dictId) {
		DynamicDictBase dynamicBase = dict_base_map.get(dictId);
		if (dynamicBase == null)
			throw new RuntimeException("没有找到对应的代码表，请确认配置中是否存在该代码。");
		return dynamicBase;
	}

}
